
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Xml;

using Swaf;
using Swaf.Xml;

namespace Swaf.Config
{
	/// <summary>
	/// Summary description for XmlElementConfigElement.
	/// </summary>
	public class XmlElementConfigElement : IConfigElement
	{
		protected XmlElement m_el;
		protected IDictionary m_context;

		public XmlElementConfigElement(IDictionary context, XmlElement el)
		{
			m_context = context;
			m_el = el;
		}

		public XmlElement Element{get{return m_el;}}

		public IDictionary Context{get{return m_context;}}

		public string getChildText()
		{
			return getChildText(null);
		}

		public string getChildText(string defaultValue)
		{
			if (m_el == null)
				return "";
			if (m_el.ChildNodes.Count == 0)
				return defaultValue;
			string p = null;
			if (m_el.FirstChild is XmlCDataSection)
				p = m_el.FirstChild.InnerText;
			else
				p = m_el.InnerXml;
			return Cmn.IsEmpty(p)?defaultValue:p;
			/*
			XmlNode p = m_el.FirstChild;
			if (p.Name != "#text")
				return defaultValue;
			return p.Value;
			*/
		}

		public string getAttribute(string name)
		{
			return getAttribute(name, null);
		}

		public string getAttribute(string name, string defaultValue)
		{
			if (m_el == null)
				return "";
			XmlAttribute p = m_el.GetAttributeNode(name);
			return (p != null)?p.Value:defaultValue;
		}

		public void checkIllegalAttributes(string[] legalAttributes)
		{
			if (m_el == null)
				return;
			XmlAttributeCollection attrs = m_el.Attributes;
			for(int p = 0; p < attrs.Count; ++p)
			{
				string a = attrs[p].Name;
				bool found = false;
				for(int p2 = 0; p2 < legalAttributes.Length; ++p2)
					if(legalAttributes[p2].Equals(a))
					{
						found = true;
						break;
					}
				if(!found)
					throw new BadConfigException(String.Format("{0}(Line {1} Pos {2}) : {3} : unexpected attribute '{4}'",
						m_context["filename"],LineNumber,LinePosition,m_context["path"], a));
			}
		}

		public IList checkUnknownAttributes(string[] legalAttributes)
		{
			IList unknowns = new ArrayList();
			if (m_el != null)
			{
				XmlAttributeCollection attrs = m_el.Attributes;
				for (int p = 0; p < attrs.Count; ++p)
				{
					string a = attrs[p].Name;
					bool found = false;
					for (int p2 = 0; p2 < legalAttributes.Length; ++p2)
						if (legalAttributes[p2].Equals(a))
						{
							found = true;
							break;
						}
					if (!found)
						unknowns.Add(new string[] { a, attrs[p].Value });
				}
			}
			return unknowns;
		}

		public int LineNumber
		{
			get
			{
				if(m_el is LineInfoXmlElement)
					return ((LineInfoXmlElement)m_el).LineNumber;
				return 0;
			}
		}
		public int LinePosition
		{
			get
			{
				if(m_el is LineInfoXmlElement)
					return ((LineInfoXmlElement)m_el).LinePosition;
				return 0;
			}
		}

	}
}
